$(function() {
	/*
	 * 添加权限组
	 */
	$(".permissionsgroup_add").on("click", function() {
		var new_pg_name = $(".new_permissionsgroup").val();
		if(new_pg_name != "") {
			var request_url = package_url("public_module/permissionsgroup/add_permissiongroup_name");
			var data = {
				"now_permissionsgroup_name": new_pg_name
			};
			crm_ajax(data, request_url, function(res) {
				if(res.status == 1) {
					var order_url = package_url("public_module/permissionsgroup/show_permission");
					crm.alert(res.msg, "success", function() {
						location.href = order_url;
					});
				} else {
					crm.alert(res.msg, "error");
				}
			})
		} else {
			crm.alert("新权限组名不能为空", "error");
		}
	})

	/*
	 * 权限组管理弹窗展示
	 */
	$(".permissionsgroup_change").on("click", function() {
		$(".permissionsgroup .permissionsgroup_top span").text("设置" + $(this).parent().parent().attr("data-name") + "的权限");
		$(".permissionsgroup").attr({
			"data-id": $(this).parent().parent().attr("data-id"),
			"data-name": $(this).parent().parent().attr("data-name")
		});
		var request_url = package_url("public_module/permissionsgroup/get_permission");
		$(".permissionsgroup_center").html("");
		//获取现有的权限并添加到页面
		crm_ajax("", request_url, function(res) {
			$.each(res, function(k, v) {
				var box = $("<div class='permissions_box'></div>");
				$("<p>" + v.name + "</p>").appendTo(box);
				$.each(v.data, function(x, y) {
					var ul = $("<ul></ul>");
					$("<li class='checkbox i-checks permissionsgroup_all'><label><input type='checkbox' /><i></i>" + y.name + "</label></li>").appendTo(ul);
					$.each(y.data, function(l, z) {
						$("<li class='checkbox i-checks'><label><input type='checkbox' data-permissions = " + k + "_" + l + "><i></i>" + z + "</label></li>").appendTo(ul);
					})
					ul.appendTo(box);
					box.appendTo($(".permissionsgroup_center"));
				})
			})
			show_now_permissions($(".permissionsgroup").attr("data-id"));
			//添加用户选中权限事件
			$(".permissionsgroup_all input[type='checkbox']").on("click", function() {
				$(this).parent().parent().siblings("li").find("input[type='checkbox']").prop("checked", $(this).prop("checked"));
			})
			$(".permissionsgroup").fadeIn();
		})
	})

	/*
	 * 显示用户现有的权限
	 */
	function show_now_permissions(now_permissionsgroup_id) {
		var request_url = package_url("public_module/permissionsgroup/get_now_permission");
		var data = {
			"now_permissionsgroup_id": now_permissionsgroup_id
		};
		var now_permissions = new Array();
		crm_ajax(data, request_url, function(res) {
			$.each(res, function(k, v) {
				now_permissions.push(v.permission);
			})
		})
		for(var j = 0; j < $(".permissions_box input[type='checkbox']").length; j++) {
			if(now_permissions.indexOf($(".permissions_box input[type='checkbox']").eq(j).attr("data-permissions")) != -1) {
				$(".permissions_box input[type='checkbox']").eq(j).prop("checked", true);
			}
		}
		for(var k = 0; k < $(".permissionsgroup_all").length; k++) {
			var select_all = true;
			for(var z = 0; z < $(".permissionsgroup_all").eq(k).siblings().length; z++) {
				if(!$(".permissionsgroup_all").eq(k).siblings().eq(z).find("input[type='checkbox']").prop("checked")) {
					select_all = false;
				}
			}
			if(select_all) {
				$(".permissionsgroup_all").eq(k).find("input[type='checkbox']").prop("checked", true);
			}
		}
	}

	/*
	 * 权限组管理提交
	 */
	$(".confirm_permissionsgroup_change").on("click", function() {
		var select_btn = false;
		var select_data = new Array();
		for(var k = 0; k < $(".permissions_box input[type='checkbox']").length; k++) {
			if($(".permissions_box input[type='checkbox']").eq(k).prop("checked")) {
				select_btn = true;
				if($(".permissions_box input[type='checkbox']").eq(k).attr("data-permissions")) {
					select_data.push($(".permissions_box input[type='checkbox']").eq(k).attr("data-permissions"));
				}
			}
		}
		if(select_btn) {
			var data = {
				"now_permissionsgroup": $(".permissionsgroup").attr("data-id"),
				"permissions": select_data
			};
			var request_url = package_url("public_module/permissionsgroup/update_permission");
			crm_ajax(data, request_url, function(res) {
				if(res) {
					crm.alert("修改成功!", "success");
				}
			})

		} else {
			crm.alert("请选择权限", "error");
		}
	})

	/*
	 * 删除权限组
	 */
	$(".permissionsgroup_delete").on("click", function() {
		var _s = $(this);
		crm.alert("是否要删除?", "warning", function() {
			var now_permissionsgroup = _s.parent().parent().attr("data-id");
			var data = {
				"id": now_permissionsgroup
			};
			var request_url = package_url("public_module/permissionsgroup/delete_permission");
			var order_url = package_url("public_module/permissionsgroup/show_permission");
			var callback = function(res) {
				if(res.status == 1) {
					crm.alert(res.msg, "success", function() {
						location.href = order_url;
					});
				} else {
					crm.alert(res.msg, "error", function() {
						location.href = order_url;
					});
				}
			};
			crm_ajax(data, request_url, callback);
		}, true);
	})

	/*
	 * 恢复权限组
	 */
	$(".permissionsgroup_restore").on("click", function() {
		var _s = $(this);
		crm.alert("是否要恢复?", "warning", function() {
			var now_permissionsgroup = _s.parent().parent().attr("data-id");
			var data = {
				"id": now_permissionsgroup
			};
			var request_url = package_url("public_module/permissionsgroup/confirm_restore");
			var order_url = package_url("public_module/permissionsgroup/restore_permission");
			var callback = function(res) {
				if(res.status == 1) {
					crm.alert(res.msg, "success", function() {
						location.href = order_url;
					});
				} else {
					crm.alert(res.msg, "error", function() {
						location.href = order_url;
					});
				}
			};
			crm_ajax(data, request_url, callback);
		}, true);
	})

	/*
	 * 权限组管理窗口关闭
	 */
	$(".permissionsgroup_exit").on("click", function() {
		$(".permissionsgroup").fadeOut();
		$(".permissions_box input[type='checkbox']").prop("checked", false);
	})
})